/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csci6450.chickenFriedFS.metaServer;

import csci6450.chickenFriedFS.nuggetCore.ChickenFile;
import csci6450.chickenFriedFS.nuggetCore.Nugget;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author jacobschoen
 */
public class MetaServerLog {

    private static MetaServerLog log;

    private List<ChickenFile> files;

    static {
        log = new MetaServerLog();
    }

    public static MetaServerLog getInstance() {
        return log;
    }

    private MetaServerLog() {
        this.files = new ArrayList<ChickenFile>();
    }

    public void format(){
        this.files = new ArrayList<ChickenFile>();
    }
    
    /**
     * Removes nugget&lt;code&gt;File&lt;/code&gt; from the nugget. Adds nugget to
     * nuggetList. Writes nugget info to log file. Syncronized.
     */
    public synchronized boolean addNuggets(List<Nugget> nugget) {
        boolean success = true;
        for (int i=0; i < nugget.size(); i=i+1){
            System.out.println("Adding Nugget Buffer Start :="+nugget.get(i).getBufferStart());
            success = success && this.addNugget(nugget.get(i));
        }
        return success;
    }

    private boolean addNugget(Nugget add){
        //check if there is a chicken file with the same name already if not
        //make one
        boolean found = false;
        for (int i=0; !found &&  i< this.files.size(); i=i+1){
            if(this.files.get(i).getFileName().equals(add.getParentFilename())){
                this.files.get(i).addNugget(add);
                found = true;
            }
        }

        if(!found){
            ChickenFile cf = new ChickenFile(add.getParentFilename(), add.getParentFileNuggetCount());
            cf.addNugget(add);
            this.files.add(cf);
        }
        return true;
    }

    public List<ChickenFile> getChickenFiles(){
        return this.files;
    }


    public List<ChickenFile> getCompleteChickenFiles(){
        System.out.println("Number of Chicken Files: "+this.files.size());
        List<ChickenFile> temp = new ArrayList<ChickenFile>();
        for (int i=0; i < this.files.size(); i=i+1){
            if(this.files.get(i).isFullyFried()){
                temp.add(this.files.get(i));
            }
        }
        return temp;
    }

    public ChickenFile getChickenFile(String filename){
        ChickenFile file = null;
        for (int i=0; i < this.files.size() && file == null; i=i+1){
            if(this.files.get(i).getFileName().equals(filename)){
                file = this.files.get(i);
            }
        }
        return file;
    }

    public void removeChickenFile(String filename){
        for (int i=0; i < this.files.size(); i=i+1){
            if(this.files.get(i).getFileName().equals(filename)){
                this.files.remove(i);
            }
        }
    }

    public List<Nugget> serverDown(String serverIp){
        List<Nugget> replicate = new ArrayList<Nugget>();

        for(int i=0; i < this.files.size(); i=i+1){
            for (int j=0; j < this.files.get(i).getNuggets().size(); j=j+1){
                if(this.files.get(i).getNuggets().get(j).getNuggetLocation().equals(serverIp)){
                    this.files.get(i).getNuggets().get(j).setNuggetLocation(this.files.get(i).getNuggets().get(j).getNuggetRepLocation());
                    this.files.get(i).getNuggets().get(j).setNuggetServerPort(this.files.get(i).getNuggets().get(j).getNuggetRepPort());
                    this.files.get(i).getNuggets().get(j).setNuggetOffset(this.files.get(i).getNuggets().get(j).getRepNuggetOffset());
                    this.files.get(i).getNuggets().get(j).setNuggetRepLocation(null);
                    this.files.get(i).getNuggets().get(j).setNuggetRepPort(-1);
                    this.files.get(i).getNuggets().get(j).setRepNuggetOffset(-1);
                    replicate.add(this.files.get(i).getNuggets().get(j));
                } else if(this.files.get(i).getNuggets().get(j).getNuggetRepLocation().equals(serverIp)) {
                    this.files.get(i).getNuggets().get(j).setNuggetRepLocation(null);
                    this.files.get(i).getNuggets().get(j).setNuggetRepPort(-1);
                    this.files.get(i).getNuggets().get(j).setRepNuggetOffset(-1);
                    replicate.add(this.files.get(i).getNuggets().get(j));
                }

            }
        }
        return replicate;
    }
}
